//
//  ChatViewCellFrame.m
//  BaiYing_Thinker
//
//  Created by 鹏 朱 on 15/11/4.
//  Copyright (c) 2015年 XKJH. All rights reserved.
//

#import "ChatViewCellFrame.h"
#import "SessonContentMessage.h"
#import "NSString+Teshuzifu.h"
#import "FileMangeHelper.h"

@interface ChatViewCellFrame()

@property (nonatomic, assign) NSInteger direction;
@property (nonatomic, assign) NSInteger horizontalWidth;
@property (nonatomic, assign) NSInteger leftTextMargin;
@property (nonatomic, assign) NSInteger leftImageMargin;

@end

@implementation ChatViewCellFrame

- (id)initWithDataModel:(SessonContentMessage *)dataModel dataModelType:(DataModelType)dataModelType webSize:(CGSize)webSize{
    
    if (self = [super init]) {
        
        self.message = dataModel;
        _dataModelType = dataModelType;
        _webViewSize = webSize;
        _horizontalWidth = kScreenWidth - kEdgeInsetLeft - kEdgeInsetBottom - kFixedClearance;
        if ([self.message.isMyself isEqual: @1]) {
            _direction = 1;
            _leftTextMargin = kEdgeInsetLeft + kFixedClearance;
            _leftImageMargin = kScreenWidth - kEdgeInsetRight - KImageWidth;
        } else {
            _direction = 0;
            _leftTextMargin = kEdgeInsetLeft;
            _leftImageMargin = kEdgeInsetLeft;
        }
    }
    
    return self;
}

- (CGRect)textRect
{
    if (_textRect.size.width <= 0 && _textRect.size.height <= 0) {
        
        CGSize size = [_message.msg_content getSizeWithWidth:_horizontalWidth font:kFontSize14];
        
        _textRect = CGRectMake(_leftTextMargin, kEdgeInsetTop, _horizontalWidth, size.height);
    }
    return _textRect;
}

- (CGRect)imageRect
{
    if (_imageRect.size.height <= 0) {
        
        if (_dataModelType == TextAndImageDataModelType) {
            _imageRect = CGRectMake(_leftImageMargin, kEdgeInsetTop + self.textRect.origin.y + self.textRect.size.height, _webViewSize.width, _webViewSize.height);
        } else {
            
            if (IsEmpty(self.message.local_Path)) {
                
                if (_webViewSize.height == 0) {
                    _imageRect = CGRectMake(_leftImageMargin, kEdgeInsetTop, KImageWidth, KImageHeight);
                } else {
                    
                    _imageRect = CGRectMake(_leftImageMargin, kEdgeInsetTop, _webViewSize.width, _webViewSize.height);
                }
                
            } else {
                
                NSString *localPath = [[FileMangeHelper shareInstance] getUploadFilePathFromFileType:UploadFileImageType fileName:_message.local_Path];
                UIImage *image = [UIImage imageWithContentsOfFile:localPath];

                //调整高度
                CGSize imgRect = [Util resetImageSizeFromSourceSize:image.size maxWidth:KImageWidth maxHeight:KImageHeight];
                
                _imageRect = CGRectMake(kScreenWidth - kEdgeInsetRight - imgRect.width, kEdgeInsetTop, imgRect.width, imgRect.height);

            }
        }
    }
    return _imageRect;
}

- (CGRect)webRect
{
    if (_webRect.size.width <= 0 && _webRect.size.height <= 0) {
        
        if (_direction == 1) {
            _webRect = CGRectMake(kScreenWidth - kEdgeInsetRight - _webViewSize.width, kEdgeInsetTop, _webViewSize.width, _webViewSize.height);
        } else {
            _webRect = CGRectMake(kEdgeInsetLeft, kEdgeInsetTop, _webViewSize.width, _webViewSize.height);
        }
        
    }
    return _webRect;
}

- (CGRect)videoRect
{
    if (_videoRect.size.width <= 0 && _videoRect.size.height <= 0) {
        
        _videoRect = CGRectMake(_leftImageMargin, kEdgeInsetTop, KImageWidth, KImageHeight);
        
    }
    return _videoRect;
}

- (CGRect)audioRect
{
    if (_audioRect.size.width <= 0 && _audioRect.size.height <= 0) {
        
        _audioRect = CGRectMake(_leftImageMargin, kEdgeInsetTop, KImageWidth, KImageHeight);
        
    }
    return _audioRect;
}

- (CGFloat)cellHeight
{
    CGFloat height = 0;
    if (_dataModelType == TextDataModelType) {
        
        height = kEdgeInsetTop + self.textRect.origin.y + self.textRect.size.height;
        
    } else if (_dataModelType == ImageDataModelType) {
        
        height = kEdgeInsetTop + self.imageRect.origin.y + self.imageRect.size.height;
        
    } else if (_dataModelType == VideoDataModelType) {
        
        height = kEdgeInsetTop + self.videoRect.origin.y + self.videoRect.size.height;
        
    } else if (_dataModelType == AudioDataModelType) {
        
//        height = kEdgeInsetTop + self.audioRect.origin.y + self.audioRect.size.height;
        height = kEdgeInsetTop + self.textRect.origin.y + self.textRect.size.height;
        
    } else if (_dataModelType == WebDataModelType) {
        
        height = kEdgeInsetTop + self.webRect.origin.y + self.webRect.size.height;
        NSLog(@"cellHeight:%f",height);
        
    } else if (_dataModelType == TextAndImageDataModelType) {
        
        height = kEdgeInsetTop + self.imageRect.origin.y + self.imageRect.size.height;
        
    }
    
    return height;
}

@end
